package com.hlj.ncp.warehouse.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.hlj.ncp.common.core.domain.AjaxResult;
import com.hlj.ncp.warehouse.entity.Product;
import com.hlj.ncp.warehouse.service.IProductService;

import java.util.*;

/**
 * 产品信息操作处理
 */
@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;

    /**
     * 产品管理页面
     */
    @GetMapping("/index")
    public String index() {
        return "product/index";
    }

    /**
     * 获取产品列表
     */
    @PreAuthorize("hasAuthority('warehouse:product:list')")
    @GetMapping("/list")
    @ResponseBody
    public AjaxResult list(Product product) {
        List<Product> list = productService.selectProductList(product);
        return AjaxResult.success(list);
    }

    /**
     * 获取产品详细信息
     */
    @PreAuthorize("hasAuthority('warehouse:product:query')")
    @GetMapping(value = "/{productId}")
    @ResponseBody
    public AjaxResult getInfo(@PathVariable("productId") Long productId) {
        return AjaxResult.success(productService.selectProductById(productId));
    }

    /**
     * 新增产品
     */
    @PreAuthorize("hasAuthority('warehouse:product:add')")
    @PostMapping
    @ResponseBody
    public AjaxResult add(@RequestBody Product product) {
        return AjaxResult.success(productService.insertProduct(product));
    }

    /**
     * 修改产品
     */
    @PreAuthorize("hasAuthority('warehouse:product:edit')")
    @PutMapping
    @ResponseBody
    public AjaxResult edit(@RequestBody Product product) {
        return AjaxResult.success(productService.updateProduct(product));
    }

    /**
     * 删除产品
     */
    @PreAuthorize("hasAuthority('warehouse:product:remove')")
    @DeleteMapping("/{productIds}")
    @ResponseBody
    public AjaxResult remove(@PathVariable Long[] productIds) {
        return AjaxResult.success(productService.deleteProductByIds(productIds));
    }
    
    /**
     * 获取产品总数量
     */
    @GetMapping("/count")
    @ResponseBody
    public AjaxResult count() {
        long count = productService.count();
        return AjaxResult.success((int)count);
    }
    
    /**
     * 获取产品图表数据
     */
    @GetMapping("/chart")
    @ResponseBody
    public AjaxResult getProductChart() {
        Map<String, Object> chartData = new HashMap<>();
        
        // 产品类别分布
        Map<String, Integer> categoryData = new HashMap<>();
        categoryData.put("蔬菜", 48);
        categoryData.put("水果", 32);
        categoryData.put("肉类", 15);
        categoryData.put("海鲜", 8);
        categoryData.put("其他", 5);
        chartData.put("categories", categoryData);
        
        // 产品状态分布
        Map<String, Integer> statusData = new HashMap<>();
        statusData.put("在售", 75);
        statusData.put("下架", 12);
        statusData.put("缺货", 8);
        statusData.put("预售", 5);
        chartData.put("status", statusData);
        
        // 月度产品新增趋势
        List<String> months = Arrays.asList("1月", "2月", "3月", "4月", "5月", "6月");
        List<Integer> newProducts = Arrays.asList(12, 19, 15, 23, 28, 25);
        Map<String, Object> trendData = new HashMap<>();
        trendData.put("months", months);
        trendData.put("data", newProducts);
        chartData.put("trend", trendData);
        
        // 产品价格区间分布
        Map<String, Integer> priceRanges = new HashMap<>();
        priceRanges.put("0-10元", 25);
        priceRanges.put("10-50元", 42);
        priceRanges.put("50-100元", 18);
        priceRanges.put("100元以上", 15);
        chartData.put("priceRanges", priceRanges);
        
        return AjaxResult.success(chartData);
    }
} 